package models

import (
	"log"
	"xorm.io/builder"
)

type School struct {
	Id                int     `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	UserId            int     `xorm:"not null comment('用户id，外键关联dts_user中的id') index INT(11)"`
	Config            int     `xorm:"not null comment('驾校配置，一个驾校可以有多个配置，此处配置为当前生效配置') INT(11)"`
	InstitutionCode   string  `xorm:"default '' comment('培训机构编号运管中心颁发初始为空') VARCHAR(16)"`
	District          string  `xorm:"not null comment('区县行政区规划码') CHAR(6)" form:"district"`
	Name              string  `xorm:"not null comment('驾校名称') VARCHAR(60)" form:"name"`
	NameShort         string  `xorm:"not null comment('驾校简称') VARCHAR(16)" form:"name_short"`
	LicenceNumber     string  `xorm:"not null comment('驾校经营许可证编号') CHAR(13)" form:"licence_number"`
	LicenceGrantTime  string  `xorm:"not null comment('驾校经营许日期 YYYYMMDD') DATE" form:"licence_grant_time"`
	BusinessNumber    string  `xorm:"not null comment('驾校营业执照注册号') CHAR(20)" form:"business_number"`
	CreditCode        string  `xorm:"default '' comment('驾校统一社会信用代码') CHAR(18)" form:"credit_code"`
	Address           string  `xorm:"not null comment('驾校地址') VARCHAR(512)" form:"address"`
	Postcode          string  `xorm:"not null comment('邮政编码') CHAR(6)" form:"postcode"`
	Legal             string  `xorm:"not null comment('法人代表') VARCHAR(64)" form:"legal"`
	Contact           string  `xorm:"not null comment('联系人') VARCHAR(64)" form:"contact"`
	Phone             string  `xorm:"not null comment('联系电话') VARCHAR(32)" form:"phone"`
	BusinessScope     string  `xorm:"not null comment('经营范围') VARCHAR(255)" form:"business_scope"`
	BusinessStatus    string  `xorm:"default '9' comment('1：营业，2：停业，3：整改，4：停业整顿，5：歇业，6：注销，9：其他') ENUM('1','2','3','4','5','6','9')" form:"business_status"`
	Level             string  `xorm:"not null comment('驾校分类等级1：一级，2：二级，3：三级') ENUM('1','2','3')" form:"level"`
	CoachCount        int64   `xorm:"not null comment('教练员总数') BIGINT(16)" form:"coach_count"`
	CarCount          int64   `xorm:"not null comment('教练车总数') BIGINT(16)" form:"car_count"`
	GrasupvNum        int64   `xorm:"default 0 comment('考核员总数') BIGINT(16)" form:"grasupv_num"`
	SafmngNum         int64   `xorm:"default 0 comment('安全员总数') BIGINT(16)" form:"safmng_num"`
	ClassroomArea     int64   `xorm:"default 0 comment('教室总面积 单位：平方米') BIGINT(16)" form:"classroom_area"`
	ThclassroomArea   int64   `xorm:"default 0 comment('理论教室面积  单位：平方米') BIGINT(16)" form:"thclassroom_area"`
	PraticefieldArea  int64   `xorm:"default 0 comment('训练场总面积 单位：平方米') BIGINT(16)" form:"praticefield_area"`
	ApprovalStatus    string  `xorm:"default '0' comment('驾校提交运管审核状态，0:待审核，1:通过审核，2:未通过审核') ENUM('0','1','2')"`
	RecordStatus      string  `xorm:"default '0' comment('备案状态 0:未备案 1:已备案') VARCHAR(1)"`
	RecordStatus2     string  `xorm:"default '0' comment('濮阳备案状态 0:未备案 1:已备案') VARCHAR(1)"`
	BaseaddStatus     string  `xorm:"default '0' comment('培训机构预录入状态 0：未预录入  1:已预录入') ENUM('0','1')"`
	Province          string  `xorm:"default '' comment('省') index CHAR(10)" form:"province"`
	City              string  `xorm:"default '' comment('市') CHAR(10)" form:"city"`
	Money             int     `xorm:"default 0 comment('可以提现总金额 单位 毫') INT(11)"`
	Registernum       int64   `xorm:"not null default 30 comment('可注册学员数') INT(11)"`
	Usednum           int64   `xorm:"not null default 0 comment('已注册学员数') INT(11)"`
	Schoolcode        string  `xorm:"comment('驾校代码') VARCHAR(255)"`
	Registeredcapital float32 `xorm:"default 0 comment('注册资金') FLOAT(20)" form:"registeredcapital"`
	Remark            string  `xorm:"comment('备注') VARCHAR(255)"`
	Signmoney         float64 `xorm:"default 50.0 comment('默认该驾校的金额') FLOAT(11,1)"`
	Remainnum         int     `xorm:"INT(11)"`
	Isused            string  `xorm:"ENUM('0','1')"`
	Zhangurl          string  `xorm:"VARCHAR(255)"`
	Lownum            int64   `xorm:"default 100 INT(11)"`
	Effective         string  `xorm:"comment('有效投诉率') VARCHAR(255)"`
	Addtime           string  `xorm:"TIMESTAMP"`
	Clicknum          int     `xorm:"INT(11)"`
	Status            int     `xorm:"default 0 comment('0为正常1为禁用') TINYINT(4)"`
	Transfertype      string  `xorm:"default '0' comment('0 Tcp 1 HTTP 传输类型') ENUM('0','1')"`
	Pacttype          string  `xorm:"default '0' comment('默认0 1代表万正  2代表驾航') ENUM('0','1','2')"`
}

type SchoolList struct {
	Id        int    `json:"id"`
	NameShort string `json:"value"`
	//Name string `json:"value"`
}

type SchoolDetail struct {
	InstitutionCode string `json:"institution_code"`
	Name            string `json:"name"`
	//Password         string `json:"password"`
	NameShort        string `json:"name_short"`
	LicenceNumber    string `json:"licence_number"`
	LicenceGrantTime string `json:"licence_grant_time"`
	BusinessNumber   string `json:"business_number"`
	Phone            string `json:"phone"`
	BusinessScope    string `json:"business_scope"`
	CreditCode       string `json:"credit_code"`
	Level            string `json:"level"`
	Legal            string `json:"legal"`
	Contact          string `json:"contact"`
	Address          string `json:"address"`
	RecordStatus     string `json:"record_status"`
	Province         string `json:"province"`
	City             string `json:"city"`
	District         string `json:"district"`
}

func (c *School) GetSchoolListByFilter(filter builder.Cond) (count int64, list []*SchoolList, err error) {
	err = engine.Table("dts_school").Where("isused = ?", "1").
		And(filter).Find(&list)
	count, _ = engine.Table("dts_school").Where("isused = ?", "1").
		And(filter).Count()
	return
}

func (c *School) GetSchoolById(id string) (school SchoolDetail, err error) {
	if _, err = engine.Table("dts_school").Alias("s").
		Select("s.institution_code,s.name,s.name_short,s.licence_number,s.record_status,s.licence_grant_time,s.business_number,s.phone,s.business_scope,s.credit_code,s.level,s.legal,s.contact,s.address,pe.province_name as province,cy.city_name as city,ct.county_name as district").
		Join("left", []string{"dts_user", "u"}, "u.id = s.user_id").
		Join("left", []string{"dts_province", "pe"}, "pe.province_id = s.province").
		Join("left", []string{"dts_city", "cy"}, "cy.city_id = s.city").
		Join("left", []string{"dts_county", "ct"}, "ct.county_id = s.district").
		Where("s.id = ?", id).
		Get(&school); err != nil {
		return school, err
	}
	return school, nil
}

type SchoolQuota struct {
	Id          int    `json:"id"`
	Name        string `json:"name"`
	NameShort   string `json:"name_short"`
	Registernum string `json:"registernum"`
	Usednum     string `json:"usednum"`
}

func (c *School) GetSchoolByFilter(filter builder.Cond) (list []*SchoolQuota, err error) {
	err = engine.Table("dts_school").Where("isused = ?", "1").And(filter).Find(&list)
	return
}

func (c *School) GetSchool(id string) (school School, err error) {
	if _, err = engine.Table("dts_school").Alias("s").ID(id).Select("s.*").Get(&school); err != nil {
		return school, err
	}
	return school, nil
}

func (c *School) EditSchool(fields ...string) bool {
	var (
		err error
	)
	_, err = engine.Table("dts_school").Id(c.Id).Cols(fields...).Update(c)
	log.Println(err)
	if err != nil {
		return false
	}
	return true
}

func (c *School) GetById(id string) (school School, err error) {
	if _, err = engine.Table("dts_school").
		Where("id = ?", id).
		Get(&school); err != nil {
		return school, err
	}
	return school, nil
}
